//
//  YZPublishWorkNoteManage.m
//  VET
//
//  Created by 刘文靖 on 2018/7/30.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPublishWorkNoteManage.h"
#import "YZNetworkManager.h"
#import "YZWorkeNoteVM.h"
#import "YZWorkNoteModel.h"
@interface YZPublishWorkNoteManage ()
@property (nonatomic, strong) YZWorkeNoteVM *viewModel;
@property (nonatomic, strong) NSMutableDictionary *noteDict;
@end
@implementation YZPublishWorkNoteManage

static YZPublishWorkNoteManage *manager = nil;
+ (YZPublishWorkNoteManage *)shareManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[YZPublishWorkNoteManage alloc] init];
    });
    
    return manager;
}
- (void)publishWorkNote:(NSDictionary *)dict mediaPath:(NSString *)mediaPath selectedPhotos:(NSArray *)selectedPhotos mediaParams:(NSDictionary *)mediaParams completion:(void (^)(void))completion{
    self.noteDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    if (mediaPath) {//视频/音频
        @weakify(self)
        [YZNetworkManager uploadVideoWithApi:@"/api/note/attachment" params:nil videoPath:mediaPath name:@"attachments" withHeaders:nil success:^(NSObject *obj) {
            @strongify(self)
            NSArray *attachments = ((NSDictionary *)obj)[@"data"];
            NSMutableArray *attachmentList = [NSMutableArray array];
            NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:mediaParams];
            params[@"attachmentUrl"] = attachments.firstObject;
            [attachmentList addObject:params];
            self.noteDict[@"attachmentList"] = attachmentList;
            [self publishNoteWithCompletion:completion];
        } fail:^(NSError *error) {
            [SVProgressHUD yz_showError:@"网络异常,请重试"];
            return ;
        }];
    }else if(selectedPhotos.count){//图片
            @weakify(self)
            [YZNetworkManager uploadImgsWithApi:@"/api/note/attachment" params:nil imgs:selectedPhotos name:@"attachments" withHeaders:nil success:^(NSObject *obj) {
                @strongify(self)
                NSArray *attachments = ((NSDictionary *)obj)[@"data"];
                NSMutableArray *attachmentList = [NSMutableArray array];
                for (int i = 0 ; i < attachments.count; i++) {
                    [attachmentList addObject:@{@"attachmentUrl":attachments[i],@"mediaType":@"image/png"}];
                }
                self.noteDict[@"attachmentList"] = attachmentList;
                [self publishNoteWithCompletion:completion];
            } fail:^(NSError *error) {
                [SVProgressHUD yz_showError:@"网络异常,请重试"];
                return ;
            }];
    }else{
        [self publishNoteWithCompletion:completion];
    }
}
- (void)publishNoteWithCompletion:(void (^)(void))completion{
    self.viewModel = [[YZWorkeNoteVM alloc] init];
    if (self.note) {
        self.noteDict[@"id"] = @(self.note.noteId);
        NSMutableArray *attachmentList = [NSMutableArray array];
        for (YZAttachmentModel *attachment in self.note.attachmentList) {
            [attachmentList addObject:@{@"attachmentUrl":attachment.attachmentUrl,@"mediaType":attachment.mediaType}];
        }
        [attachmentList addObjectsFromArray:self.noteDict[@"attachmentList"]];
        self.noteDict[@"attachmentList"] = attachmentList;
        [[self.viewModel updateNote:self.noteDict] subscribeError:^(NSError * _Nullable error) {
            [SVProgressHUD yz_showErrorInfo:error];
        } completed:^{
            [SVProgressHUD yz_showMessage:@"更新成功"];
            if (completion) {
                completion();
            }
        }];
    }else{
        [[self.viewModel createNote:self.noteDict] subscribeError:^(NSError * _Nullable error) {
           [SVProgressHUD yz_showErrorInfo:error];
        } completed:^{
            [SVProgressHUD yz_showMessage:@"发布成功"];
            if (completion) {
                completion();
            }
        }];
    }
}
@end
